<?php
	## by ake 08/2012##
	
 class Manage_info extends CI_Controller{
 	
 	public function __construct(){
 		parent::__construct();
 		$this->load->library('session');
 		$this->load->library('form_validation');
 		$this->load->library('cismarty');
 		$this->load->model("apanel/template");
 		$this->load->model("apanel/db_info");
 		$this->load->library('pagination');
 	}
 	
 	public function index(){
 		redirect('manage_info/info');
 	}
 	#=== department
 	
 	public function info($st=""){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
//  		list($cat,$num,$page,$total) = $this->db_hotel->department_list();
//  		$this->cismarty->assign("Num_Rows",$num);
//  		$this->cismarty->assign("cat",$cat);
//  		$this->cismarty->assign("pagelink",$page);
//  		$this->cismarty->assign("total",$total);
 		if($st == "success"){
 			$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
 		}
 			
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open('manage_info/department_del_all'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/info/info.html');
 	}
 	
 	public function department_add(){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($this->input->post('bntSubmit') != null){
 			$this->form_validation->set_rules("title_en","Category name","required");
 			$this->form_validation->set_rules("title_cn","Category name","trim");
 			$this->form_validation->set_rules("title_th","Category name","trim");
 			if($this->form_validation->run() == FALSE){
 				$this->cismarty->assign("title_en",set_value('title_en'));
 				$this->cismarty->assign("title_cn",set_value('title_cn'));
 				$this->cismarty->assign("title_th",set_value('title_th'));
 					
 				//set error
 				$this->cismarty->assign("errortitle_en",form_error('title_en'));
 			}else {
 				$data = array(
 						"name_en"=>trim($this->input->post('title_en')),
 						"name_zh_cn"=>trim($this->input->post('title_cn')),
 						"name_th"=>trim($this->input->post('title_th')),
 						"created_by"=>$this->session->userdata("sess_username"),
 						"created_date"=>date("Y-m-d H:i:s")
 				);
 				$res = $this->db->insert('hotel_department',$data);
 				if($res) redirect('manage_hotel/hotel_department/success');
 				else  redirect('manage_hotel/hotel_department/fail');
 			}
 			//$this->form_validation->set_rules("","","");
 		}
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open('manage_hotel/department_add'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/hotel/department_add.html');
 	}
 	
 	public function department_edit($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($this->input->post('bntSubmit') != null){
 			$this->form_validation->set_rules("title_en","Category name","required");
 			$this->form_validation->set_rules("title_cn","Category name","trim");
 			$this->form_validation->set_rules("title_th","Category name","trim");
 			if($this->form_validation->run() == FALSE){
 				$this->cismarty->assign("title_en",set_value('title_en'));
 				$this->cismarty->assign("title_cn",set_value('title_cn'));
 				$this->cismarty->assign("title_th",set_value('title_th'));
 		
 				//set error
 				$this->cismarty->assign("errortitle_en",form_error('title_en'));
 			}else {
 				$data = array(
 						"name_en"=>trim($this->input->post('title_en')),
 						"name_zh_cn"=>trim($this->input->post('title_cn')),
 						"name_th"=>trim($this->input->post('title_th')),
 						"updated_by"=>$this->session->userdata("sess_username"),
 						"updated_date"=>date("Y-m-d H:i:s")
 				);
 				$this->db->where("pkid",$id);
 				$res = $this->db->update('hotel_department',$data);
 				if($res) redirect('manage_hotel/hotel_department/success');
 				else  redirect('manage_hotel/hotel_department/fail');
 			}
 			//$this->form_validation->set_rules("","","");
 		}else {
 			$this->db->where("pkid",$id);
 			$res = $this->db->get("hotel_department");
 			$row = $res->row();
 			$this->cismarty->assign("title_en",$row->name_en);
 			$this->cismarty->assign("title_cn",$row->name_zh_cn);
 			$this->cismarty->assign("title_th",$row->name_th);
 			
 		}
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open("manage_hotel/department_edit/$id"));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/hotel/department_edit.html');
 	}
 	
 	function department_del($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($id != ""){
 			$this->db->where('pkid',$id);
 			$res = $this->db->delete("hotel_department");
 			if($res) redirect('manage_hotel/hotel_department/success');
 		}
 	}
 	
 	public function department_del_all(){
 		$check = $this->input->post('check');
 		if($check != ""){
 			foreach ($check as $index){
 				$this->db->where('pkid',$index);
 				$res = $this->db->delete("hotel_department");
 			}
 		}
 		redirect('manage_hotel/hotel_department/success');
 	}
 	
 	#=== partner===
 	
 	public function partner_list($st = ""){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		list($cat,$num,$page,$total) = $this->db_hotel->partner();
 		$this->cismarty->assign("Num_Rows",$num);
 		$this->cismarty->assign("cat",$cat);
 		$this->cismarty->assign("pagelink",$page);
 		$this->cismarty->assign("total",$total);
 		if($st == "success"){
 			$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
 		}
 		
 		//== template
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2,2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open('manage_hotel/partner_del_all'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/hotel/partner_list.html');
 	}
 	
 	public function partner_add(){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($this->input->post("bntSubmit") != null){
 			$this->form_validation->set_rules("status","Status","required");
 			$this->form_validation->set_rules("name","Name","required");
 			$this->form_validation->set_rules("web","Website",'trim');
 			$this->form_validation->set_rules("email","Email","trim|valid_email");
 			$this->form_validation->set_rules("address1","Address",'trim');
 			$this->form_validation->set_rules("address2","Address2",'trim');
 			$this->form_validation->set_rules("country","Country","trim");
 			$this->form_validation->set_rules("contact_no","contact_no","trim");
 			$this->form_validation->set_rules("contact","contact","trim");
 			$this->form_validation->set_rules("lang","lang","trim");
 			if($this->form_validation->run() == FALSE){
 				$this->cismarty->assign("name",set_value('name'));
 				$this->cismarty->assign("web",set_value('web'));
 				$this->cismarty->assign("email",set_value('email'));
 				$this->cismarty->assign("address1",set_value('address1'));
 				$this->cismarty->assign("address2",set_value('address2'));
 				$this->cismarty->assign("contact_no",set_value('contact_no'));
 				$this->cismarty->assign("contact",set_value('contact'));
 				
 				//set error
 				$this->cismarty->assign("errorstatus",form_error('status'));
 				$this->cismarty->assign("errorname",form_error('name'));
 				$this->cismarty->assign("erroremail",form_error('email'));
 			}else {
 				$error = false;
 				if($_FILES['pic']['tmp_name'] != ""){
 					$config['upload_path'] = 'media/logo/';
 					$config['allowed_types'] = 'gif|jpg|png';
 					$config['max_size']	= '1024'; // kb
 					$this->load->library('upload', $config);
 					if ( $this->upload->do_upload('pic'))
 					{
 						$data =  $this->upload->data();
 						$picname = date('YmdHis').$data['file_ext'];
 						rename($data['full_path'], $data['file_path'].$picname);
 						$config['image_library'] ='gd2';
 						$config['source_image'] =  $data['file_path'].$picname;
 						$config['width']	= 250;
 						$config['height'] = 250;
 						$this->load->library('image_lib', $config);
 						$this->image_lib->resize();
 					}else{
 						//$pic_error = array('error' => $this->upload->display_errors());
 						$this->cismarty->assign("errorpic",$this->upload->display_errors());
 						$error = true;
 					}
 				}
 				if(!$error){
 				$data = array(
 						"name"=>$this->input->post('name'),
 						"website"=>$this->input->post('web'),
 						"email"=>$this->input->post('email'),
 						"contact_person"=>$this->input->post('contact'),
 						"address1"=>$this->input->post('address1'),
 						"address2"=>$this->input->post('address2'),
 						"country_pkid"=>$this->input->post('country'),
 						"contact_no"=>$this->input->post('contact_no'),
 						"logo"=>$picname,
 						"default_language"=>$this->input->post('lang'),
 						"status"=>$this->input->post('status'),
 						"created_by"=>$this->session->userdata("sess_username"),
 						"created_date"=>date('Y-m-d H:i:s')
 						);
 					$res =$this->db->insert('hotel_partner',$data);
 					if($res) redirect('manage_hotel/partner_list/success');
	 				else  redirect('manage_hotel/partner_list/fail');
 				}
 			}
 			
 		}
 		//== template
 		$this->cismarty->assign("str_country",$this->db_hotel->country_list());
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2,2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open_multipart('manage_hotel/partner_add'));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/hotel/partner_add.html');
 	}
 	
 	public function partner_edit($id){
 			if($this->session->userdata('sess_login') == null)
 				redirect('apanel');
 			if($this->input->post("bntSubmit") != null){
 				$this->form_validation->set_rules("status","Status","required");
 				$this->form_validation->set_rules("name","Name","required");
 				$this->form_validation->set_rules("web","Website",'trim');
 				$this->form_validation->set_rules("email","Email","trim|valid_email");
 				$this->form_validation->set_rules("address1","Address",'trim');
 				$this->form_validation->set_rules("address2","Address2",'trim');
 				$this->form_validation->set_rules("country","Country","trim");
 				$this->form_validation->set_rules("contact_no","contact_no","trim");
 				$this->form_validation->set_rules("contact","contact","trim");
 				$this->form_validation->set_rules("lang","lang","trim");
 				if($this->form_validation->run() == FALSE){
 					$this->cismarty->assign("name",set_value('name'));
 					$this->cismarty->assign("web",set_value('web'));
 					$this->cismarty->assign("email",set_value('email'));
 					$this->cismarty->assign("address1",set_value('address1'));
 					$this->cismarty->assign("address2",set_value('address2'));
 					$this->cismarty->assign("contact_no",set_value('contact_no'));
 					$this->cismarty->assign("contact",set_value('contact'));
 						
 					//set error
 					$this->cismarty->assign("errorstatus",form_error('status'));
 					$this->cismarty->assign("errorname",form_error('name'));
 					$this->cismarty->assign("erroremail",form_error('email'));
 				}else {
 					$error = false;
 					if($_FILES['pic']['tmp_name'] != ""){
 						$config['upload_path'] = 'media/logo/';
 						$config['allowed_types'] = 'gif|jpg|png';
 						$config['max_size']	= '1024'; // kb
 						$this->load->library('upload', $config);
 						if ( $this->upload->do_upload('pic'))
 						{
 							$data =  $this->upload->data();
 							$picname = date('YmdHis').$data['file_ext'];
 							rename($data['full_path'], $data['file_path'].$picname);
 							$config['image_library'] ='gd2';
 							$config['source_image'] =  $data['file_path'].$picname;
 							$config['width']	= 250;
 							$config['height'] = 250;
 							$this->load->library('image_lib', $config);
 							$this->image_lib->resize();
 							$this->db->where('pkid',$id);
 							$uppic = array("logo"=>$picname);
 							$this->db->update('hotel_partner',$uppic);
 						}else{
 							//$pic_error = array('error' => $this->upload->display_errors());
 							$this->cismarty->assign("errorpic",$this->upload->display_errors());
 							$error = true;
 						}
 					}
 					if(!$error){
 						$data = array(
 								"name"=>$this->input->post('name'),
 								"website"=>$this->input->post('web'),
 								"email"=>$this->input->post('email'),
 								"contact_person"=>$this->input->post('contact'),
 								"address1"=>$this->input->post('address1'),
 								"address2"=>$this->input->post('address2'),
 								"country_pkid"=>$this->input->post('country'),
 								"contact_no"=>$this->input->post('contact_no'),
 								"default_language"=>$this->input->post('lang'),
 								"status"=>$this->input->post('status'),
 								"updated_by"=>$this->session->userdata("sess_username"),
 								"updated_date"=>date('Y-m-d H:i:s')
 						);
 						$this->db->where('pkid',$id);
 						$res =$this->db->update('hotel_partner',$data);
 						if($res) redirect('manage_hotel/partner_list/success');
 						else  redirect('manage_hotel/partner_list/fail');
 					}
 				}
 		
 			}else {
 				$this->db->where('pkid',$id);
 				$res = $this->db->get('hotel_partner');
 				$row = $res->row();
 				$this->cismarty->assign("name",$row->name);
 				$this->cismarty->assign("web",$row->website);
 				$this->cismarty->assign("email",$row->email);
 				$this->cismarty->assign("address1",$row->contact_person);
 				$this->cismarty->assign("address1",$row->address1);
 				$this->cismarty->assign("address2",$row->address2);
 				$this->cismarty->assign("contact_no",$row->contact_no);
 				$this->cismarty->assign("contact",$row->contact_person);
 				$country = $row->country_pkid;
 				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
 				else  $this->cismarty->assign("sd",'checked="checked"');
 				$lang 	= 	$row->default_language;
 				if(trim($row->logo) != "")
 				$this->cismarty->assign('img','<img src="'.base_url().'media/logo/'.$row->logo.'"><br>');
 			}
 			//== template
 			$this->cismarty->assign("langs",$this->db_hotel->langs($lang));
 			$this->cismarty->assign("str_country",$this->db_hotel->country_list($country));
 			$this->cismarty->assign("header",$this->template->header());
 			$this->cismarty->assign("menu",$this->template->menu(2,6));
 			$this->cismarty->assign("footer",$this->template->footer());
 			$this->cismarty->assign("url",base_url());
 			$this->cismarty->assign("form_open",form_open_multipart("manage_hotel/partner_edit/$id"));
 			$this->cismarty->assign("form_close",form_close());
 			$this->cismarty->display('apanel/hotel/partner_edit.html');

 	}
 	
 	public function partner_del($id){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($id != ""){
 			$this->db->where('pkid',$id);
 			$res = $this->db->delete("hotel_partner");
 			if($res) redirect('manage_hotel/partner_list/success');
 		}
 	}
 	
 	public function partner_del_all(){
 		$check = $this->input->post('check');
 		if($check != ""){
 			foreach ($check as $index){
 				$this->db->where('pkid',$index);
 				$res = $this->db->delete("hotel_partner");
 			}
 		}
 		redirect('manage_hotel/partner_list/success');
 	}
 	
 	public function customer_info($st=""){
 		if($this->session->userdata('sess_login') == null)
 			redirect('apanel');
 		if($st == "success"){
 			$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
 		}
 		$country = "";
 			$this->db->select('pkid');
 			$resrow =$this->db->get('mobile_customer');
 			$row = $resrow->row();
 			$pkid = $row->pkid;
 			//update
 			if($this->input->post("bntSubmit") != null){
		 			$this->form_validation->set_rules("name","Name","required");
		 			$this->form_validation->set_rules("phone","Phone","trim");
		 			$this->form_validation->set_rules("fax","fax","trim");
		 			$this->form_validation->set_rules("web","web","trim");
		 			$this->form_validation->set_rules("email","Email","trim|valid_email");
		 			$this->form_validation->set_rules("facebook","facebook","trim");
		 			$this->form_validation->set_rules("contact","contact","trim");
		 			$this->form_validation->set_rules("email2","Contact Email","trim|valid_email");
		 			$this->form_validation->set_rules("address1","address1","trim");
		 			$this->form_validation->set_rules("address2","address2","trim");
		 			$this->form_validation->set_value('desc_en','Description En','trim');
		 			$this->form_validation->set_value('desc_cn','Description Cn','trim');
		 			$this->form_validation->set_value('desc_th','Description Th','trim');
		 			
		 			if($this->form_validation->run() == FALSE){
		 				$this->cismarty->assign("name",set_value('name'));
		 				$this->cismarty->assign("phone",set_value('phone'));
		 				$this->cismarty->assign("fax",set_value('fax'));
		 				$this->cismarty->assign("web",set_value('web'));
		 				$this->cismarty->assign("email",set_value('email'));
		 				$this->cismarty->assign("facebook",set_value('facebook'));
		 				$this->cismarty->assign("contact",set_value('contact'));
		 				$this->cismarty->assign("email2",set_value('email2'));
		 				$this->cismarty->assign("address1",set_value('address1'));
		 				$this->cismarty->assign("address2",set_value('address2'));
		 				$this->cismarty->assign("desc_en",set_value('desc_en'));
		 				$this->cismarty->assign("desc_cn",set_value('desc_cn'));
		 				$this->cismarty->assign("desc_th",set_value('desc_th'));
		 					
		 				//set error
		 				$this->cismarty->assign("errorname",form_error('name'));
		 				$this->cismarty->assign("erroremail",form_error('email'));
		 				$this->cismarty->assign("erroremail2",form_error('email2'));
		 					
		 			}else{
		 				$error = false;
		 				if($_FILES['pic']['tmp_name'] != ""){
		 					$config['upload_path'] = 'media/room/';
		 					$config['allowed_types'] = 'gif|jpg|png';
		 					$config['max_size']	= '1024'; // kb
		 					$config['create_thumb'] = TRUE;
		 					$this->load->library('upload', $config);
		 					if ( $this->upload->do_upload('pic'))
		 					{
		 						$data =  $this->upload->data();
		 						$picname = date('YmdHis').$data['file_ext'];
		 						$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
		 						rename($data['full_path'], $data['file_path'].$picname);
		 						$config['image_library'] ='gd2';
		 						$config['source_image'] =  $data['file_path'].$picname;
		 						$config['width']	= 350;
		 						$config['height'] = 100;
		 						$this->load->library('image_lib', $config);
		 						$this->image_lib->resize();
		 						$uppic = array('img'=>$picname);
		 						$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
		 						$this->db->update('mobile_customer',$uppic);
		 					}else{
		 						//$pic_error = array('error' => $this->upload->display_errors());
		 						$this->cismarty->assign("errorpic",$this->upload->display_errors());
		 						$error = true;
		 					}
		 				}
		 				if(!$error){
			 				$data = array(
			 						"name"=>$this->input->post('name'),
			 						"phone"=>$this->input->post('phone'),
			 						"fax"=>$this->input->post('fax'),
			 						"email"=>$this->input->post('email'),
			 						"website"=>$this->input->post('web'),
			 						"address1"=>$this->input->post('address1'),
			 						"address2"=>$this->input->post('address2'),
			 						"description_en"=>addslashes($this->input->post('desc_en')),
			 						"description_zh_cn"=>addslashes($this->input->post('desc_cn')),
			 						"description_th"=>addslashes($this->input->post('desc_th')),
			 						"country_pkid"=>$this->input->post('country'),
			 						"facebook"=>$this->input->post('facebook'),
			 						"twitter"=>$this->input->post('twitter'),
			 						"blog"=>$this->input->post('blog'),
			 						"contact_person"=>$this->input->post('contact'),
			 						"contact_email"=>$this->input->post('email2'),
			 						"updated_by"=>$this->session->userdata("sess_username"),
			 						"updated_date"=>date('Y-m-d H:i:s'),
			 				);
			 				$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
			 				$res = $this->db->update("mobile_customer",$data);
			 				if($res) redirect('manage_info/customer_info/success');
			 				else  redirect('manage_info/customer_info/fail');
			 			}
		 			}
 			}//end post
 			else{
 				$this->db->where("pkid",$this->session->userdata("sess_customer_id"));
 				$res = $this->db->get('mobile_customer');
 				$row = $res->row();
 				$this->cismarty->assign("name",$row->name);
 				$this->cismarty->assign("phone",$row->phone);
 				$this->cismarty->assign("fax",$row->fax);
 				$this->cismarty->assign("web",$row->website);
 				$this->cismarty->assign("email",$row->email);
 				$this->cismarty->assign("facebook",$row->facebook);
 				$this->cismarty->assign("twitter",$row->twitter);
 				$this->cismarty->assign("blog",$row->blog);
 				$this->cismarty->assign("contact",$row->contact_person);
 				$this->cismarty->assign("email2",$row->contact_email);
 				$this->cismarty->assign("address1",stripslashes($row->address1));
 				$this->cismarty->assign("address2",stripslashes($row->address2));
 				$country = $row->country_pkid;
 				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
 				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
 				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
 				if(trim($row->img) != "") $this->cismarty->assign("img",'<img src="'.base_url().'media/room/'.$row->img.'"><br>');
 			}

 		$this->cismarty->assign("tiny",$this->template->tinymce());
 		//== template
 		$this->cismarty->assign("str_country",$this->db_info->country_list($country));
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(2));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open_multipart("manage_info/customer_info"));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/info/info.html');
 			
 	}
 	
 }